package classification;

import java.util.Hashtable;

import basics.Dataset;
import basics.DatasetItem;
import basics.Vector;
import basics.VectorMatrix;

public class NaiveBayes extends Classifier<Integer> {

	private Hashtable<Integer, Vector> _mean;
	private Hashtable<Integer, Vector> _variance;
	private int _n = 0;

	public void update(Vector x, int y) {
		fillParam(_mean, x, y);
		fillParam(_variance, x, y);
		_n++;
		Vector prevMean = _mean.get(y);

		_mean.get(y).addEqual(x.minus(prevMean).times(1. / (double) _n));
		_variance.put(y, (Vector) (_variance.get(y).times(_n - 1).add(x.minus(_mean.get(y)).timesEqual(
				x.minus(prevMean)))).times(1. / _n));
	}

	private void fillParam(Hashtable<Integer, Vector> p, Vector x, int y) {
		Vector f;
		if (!p.containsKey(y)) {
			f = x.copy();
			f.set(0.);
			p.put(y, f);
		}
	}

	@Override
	public void train(Dataset<Vector, Integer> ds) {
		for (int i = 0; i < ds.size(); i++) {
			update(ds.feature(i), ds.classVal(i));
		}
	}

	@Override
	public Integer predict(VectorMatrix x) {
		return null;
	}
}
